package com.gqj.bas.controller;

import com.gqj.bas.domain.BasDevice;
import com.gqj.bas.service.IBasDeviceService;
import com.gqj.common.annotation.Log;
import com.gqj.common.constant.UserConstants;
import com.gqj.common.core.controller.BaseController;
import com.gqj.common.core.domain.AjaxResult;
import com.gqj.common.core.page.TableDataInfo;
import com.gqj.common.enums.BusinessType;
import com.gqj.common.utils.poi.ExcelUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 设备Controller
 * 
 * @author lxq
 * @date 2023-12-17
 */
@RestController
@RequestMapping("/bas/basDevice")
public class BasDeviceController extends BaseController
{
    @Autowired
    private IBasDeviceService basDeviceService;

    /**
     * 查询设备列表
     */
    @PreAuthorize("@ss.hasPermi('bas:basDevice:list')")
    @GetMapping("/list")
    public TableDataInfo list(BasDevice basDevice)
    {
        startPage();
        basDevice.setDelFlag("0");
        List<BasDevice> list = basDeviceService.selectBasDeviceList(basDevice);
        return getDataTable(list);
    }

    /**
     * 导出设备列表
     */
    @PreAuthorize("@ss.hasPermi('bas:basDevice:export')")
    @Log(title = "设备", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BasDevice basDevice)
    {
        List<BasDevice> list = basDeviceService.selectBasDeviceList(basDevice);
        ExcelUtil<BasDevice> util = new ExcelUtil<BasDevice>(BasDevice.class);
        util.exportExcel(response, list, "设备数据");
    }

    /**
     * 获取设备详细信息
     */
    @PreAuthorize("@ss.hasPermi('bas:basDevice:query')")
    @GetMapping(value = "/{deviceId}")
    public AjaxResult getInfo(@PathVariable("deviceId") Long deviceId)
    {
        return AjaxResult.success(basDeviceService.selectBasDeviceByDeviceId(deviceId));
    }

    /**
     * 新增设备
     */
    @PreAuthorize("@ss.hasPermi('bas:basDevice:add')")
    @Log(title = "设备", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BasDevice basDevice)
    {
        if (UserConstants.NOT_UNIQUE.equals(basDeviceService.checkDeviceCodeUnique(basDevice)))
        {
            return AjaxResult.error("新增设备'" + basDevice.getDeviceName() + "'失败，设备编码已存在");
        }
        if (UserConstants.NOT_UNIQUE.equals(basDeviceService.checkDeviceIpUnique(basDevice)))
        {
            return AjaxResult.error("新增设备'" + basDevice.getDeviceName() + "'失败，ip已存在");
        }
        basDevice.setCreateBy(getUsername());
        return toAjax(basDeviceService.insertBasDevice(basDevice));
    }

    /**
     * 修改设备
     */
    @PreAuthorize("@ss.hasPermi('bas:basDevice:edit')")
    @Log(title = "设备", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BasDevice basDevice)
    {
        if (UserConstants.NOT_UNIQUE.equals(basDeviceService.checkDeviceCodeUnique(basDevice)))
        {
            return AjaxResult.error("修改设备'" + basDevice.getDeviceName() + "'失败，设备编码已存在");
        }
        if (UserConstants.NOT_UNIQUE.equals(basDeviceService.checkDeviceIpUnique(basDevice)))
        {
            return AjaxResult.error("修改设备'" + basDevice.getDeviceName() + "'失败，ip已存在");
        }
        basDevice.setUpdateBy(getUsername());
        return toAjax(basDeviceService.updateBasDevice(basDevice));
    }

    /**
     * 删除设备
     */
    @PreAuthorize("@ss.hasPermi('bas:basDevice:remove')")
    @Log(title = "设备", businessType = BusinessType.DELETE)
	@DeleteMapping("/{deviceIds}")
    public AjaxResult remove(@PathVariable Long[] deviceIds)
    {
        return toAjax(basDeviceService.deleteBasDeviceByDeviceIds(deviceIds));
    }
}
